##### Tables A5 and A6 script #####
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(gt)
library(Hmisc)
library(tidyverse)
library(dplyr)
library(hrbrthemes)
library(lfe)
library(stargazer)

here::i_am("Scripts/TabA5A6Script.R")

library(here)

#Enter Data and filter to Individuals####
df<-readRDS(here("Data","descriptive_donation_data.rds"))



#Get just individuals
finall<-df%>%
  filter(Type_of_Contributor=="Individual")

#Match the offices we use elsewhere (remove Appellate Court since we don't use anywhere else)

finall<-finall%>%
  filter(cOFFICE!="APPELLATE COURT")


# Constructing TABLES####

## Event vs. Non-Event By State and Time ####
#By election period


#KY
finall<-finall%>%
  mutate(eleccycnum=ifelse(cSTATE=="KY" & cEYEAR<2000, 1,
                           ifelse(cSTATE=="KY" & cEYEAR>1999& cEYEAR<2004, 2,
                                  ifelse(cSTATE=="KY" & cEYEAR>2003& cEYEAR<2008, 3,
                                         ifelse(cSTATE=="KY" & cEYEAR>2007& cEYEAR<2012, 4,
                                                ifelse(cSTATE=="KY" & cEYEAR>2011& cEYEAR<2016, 5,
                                                       ifelse(cSTATE=="KY" & cEYEAR>2015& cEYEAR<2020, 6,NA)))))))


#WV
finall<-finall%>%
  mutate(eleccycnum=ifelse(cSTATE=="WV" & cEYEAR<2001, 1,
                           ifelse(cSTATE=="WV" & cEYEAR>2000& cEYEAR<2005, 2,
                                  ifelse(cSTATE=="WV" & cEYEAR>2004& cEYEAR<2009, 3,
                                         ifelse(cSTATE=="WV" & cEYEAR>2008& cEYEAR<2013, 4,
                                                ifelse(cSTATE=="WV" & cEYEAR>2012& cEYEAR<2017, 5,eleccycnum))))))

#MI/OH
finall<-finall%>%
  mutate(eleccycnum=ifelse((cSTATE=="OH"|cSTATE=="MI") & cEYEAR<2003, 1,
                           ifelse((cSTATE=="OH"|cSTATE=="MI") & cEYEAR>2002& cEYEAR<2007, 2,
                                  ifelse((cSTATE=="OH"|cSTATE=="MI") & cEYEAR>2006& cEYEAR<2011, 3,
                                         ifelse((cSTATE=="OH"|cSTATE=="MI") & cEYEAR>2010& cEYEAR<2015, 4,
                                                ifelse((cSTATE=="OH"|cSTATE=="MI") & cEYEAR>2014& cEYEAR<2019, 5,eleccycnum))))))



#Get the things we need for table
fintab<-finall%>%
  select(eleccycnum, cSTATE, fundraiser)%>%
  group_by(eleccycnum, cSTATE)%>%
  dplyr::summarize(num=n(), percevent=mean(fundraiser, na.rm=T)) 

fintab$eleccycnum<-factor(fintab$eleccycnum, labels=c( "Cycle 1", "Cycle 2", "Cycle 3", "Cycle 4", "Cycle 5", "Cycle 6"))
fintab$State<-factor(fintab$cSTATE, levels=c("KY", "MI", "OH", "WV"))

#Remove the Michigan NAs

fintab<-fintab%>%
  filter(!is.na(eleccycnum))

#Raw number of donations

fintab2<-fintab %>%
  pivot_wider(
    id_cols = State,
    names_from = eleccycnum,
    values_from = percevent
  )


lattable<-fintab2%>%
  gt()%>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  )%>%
  fmt_auto()

lattable |> gtsave(here("Results","TabA5.tex"))

#Monetary Amount (Table A6)

#Monetary Amount of donations

fintabamt<-finall%>%
  select(eleccycnum, cSTATE, fundraiser,cAMOUNT)%>%
  group_by(eleccycnum, cSTATE)%>%
  dplyr::summarize(sumtot=sum(cAMOUNT), sumeven=sum(cAMOUNT[fundraiser==1]), percevent=sumeven/sumtot) 

fintabamt$eleccycnum<-factor(fintabamt$eleccycnum, labels=c( "Cycle 1", "Cycle 2", "Cycle 3", "Cycle 4", "Cycle 5", "Cycle 6"))
fintabamt$State<-factor(fintabamt$cSTATE, levels=c("KY", "MI", "OH", "WV"))

#Remove the Michigan NAs

fintab<-fintabamt%>%
  filter(!is.na(eleccycnum))


fintab2<-fintab %>%
  pivot_wider(
    id_cols = State,
    names_from = eleccycnum,
    values_from = percevent
  )


lattable<-fintab2%>%
  gt()%>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  )%>%
  fmt_auto()

lattable |> gtsave(here("Results","TabA6.tex"))
